import {Component, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Params} from '@angular/router';
import {Observable, Subject} from 'rxjs';
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {ShareService} from "@share/share.service";

@Component({
  selector: 'seed-live-broadcast',
  templateUrl: './live-broadcast.component.html',
  styleUrls: ['./live-broadcast.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class LiveBroadcastComponent implements OnInit, OnDestroy {
  public bg = '';
  public liveAdvUrl = '';
  public defaulticon = '';
  public info$: Observable<any>;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public live = {
    isMaster: '',
    liveId: '',
  };
  public videoId = '';
  public data = {
    icon: '',
    name: ''
  };

  constructor(
    private shareSer: ShareService,
    private route: ActivatedRoute,
    private tools: ToolsService,
    private imgSer: ImageServeService
  ) {
  }

  ngOnInit() {
    this.liveAdvUrl = this.imgSer.getliveAdvUrl();
    this.defaulticon = this.imgSer.getUserIcon();
    this.dataInit();
  }

  dataInit() {
    this.info$ = this.shareSer._liveBroadcast;
    this.info$
      .takeUntil(this.ngUnsubscribe)
      .subscribe((res) => {
        if (this.live['isMaster'] && res['result'] === '0') {
          this.data.icon = res.data['iconUrl'];
          this.data.name = res.data['nickName'];
          this.bg = res.data['showImg'];
        }
        if (this.videoId && res['result'] === '0') {
          this.data.icon = res.data['userIcon'];
          this.data.name = res.data['nickName'];
          this.bg = res.data['videoCover'];
        }
      });

    this.route.params
      .switchMap((params: Params) => {
        this.live['liveId'] = params['liveId'];
        this.videoId = params['liveId'];
        return this.route.queryParams;
      }).subscribe((params: Params) => {
        if (params['type'] === 'live' && params['isMaster']) {
          this.live['isMaster'] = params['isMaster'];
          // this.live['liveId'] = params['liveId'];
          this.shareSer.getLiveBroadcast(this.live['liveId']);
          return;
        }
        if (params['type'] === 'video') {
          this.shareSer.getVideo(this.videoId);
        }
      });
  }

  downloadAppTip() {
    this.tools.presentConfirm('下载求苗APP，看更多精彩内容！');
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
